﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class MainViewModel
    {

        public DataRow NewABA()
        {
            DataRow dwData = INTEXDB.ICC_ABAX.NewRow();
            foreach (DataColumn cm in INTEXDB.ICC_ABAX.Columns)
            {
                if (dwData[cm.ColumnName].ToString() == "")
                {
                    if (cm.DataType.ToString().ToLower().Contains("string"))
                    {
                        dwData[cm.ColumnName] = "".PadRight(cm.MaxLength);
                    }
                    else
                    {
                        dwData[cm.ColumnName] = 0;
                    }
                }
            }
            return dwData;
        }
        /// <summary>
        /// 1.自动生产生产单所需要的必要数据
        /// 2.利用ICC_ABAX表明生产单之间的计划关系
        /// 3.产生原料需求单
        /// </summary>
        /// <param name="dwInput"></param>
        /// <param name="src"></param>
        /// <returns></returns>
        double R01 = 0, R02 = 0, F01 = 0, F02 = 0, F02DES=0,JNUM = 0, JWASTGE = 0;
        double QTYEND = 0;
        double QTYMAT = 0;
        string DELDATE = "0";

        public DataSet PrePlan(DataSet SetEnv, DataSet db, DataRow dwVKPOS)
        {
            string krs = "", lfd = "", pos = "";
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            ABPOS = 1;
            tbABAUFM = db.Tables["ICC_ABAX"].Clone();
            DataRow dwMU;
            krs = dwVKPOS["VKPAUFKRS"].ToString();
            lfd = dwVKPOS["VKPAUFLFD"].ToString();
            pos = dwVKPOS["VKPAUFPOS"].ToString();
            string filter = "";
            DataRow[] rows;
            #region======Requirement=======
            DataRow dwVKWDECK = null;
            filter = "WDAUFKRS='" + krs + "' AND WDAUFLFD='"
               + lfd + "' AND WDAUFPOS='" + pos + "' AND WDSA='K' AND WDUPOS='00'";
            rows = db.Tables["WDECK"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwVKWDECK = row;
                break;
            }
            #endregion
            #region=======RUELS============================
            var qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                         where row["ARTSTAGE"].ToString().Trim() == "S"
                         select row;
            DataRow dwRule = null;
            foreach (DataRow row in qRules)
            {
                dwRule = row;
            }

            QTYEND = 0; QTYMAT = 0; R01 = 0; F01 = 0;
            if (dwRule != null)
            {
                R01 = Convert.ToDouble(dwRule["R01"].ToString()) / 100;
                F01 = Convert.ToDouble(dwRule["F01"].ToString());
                DELDATE = dwRule["DELDATE"].ToString();
                QTYEND = Convert.ToDouble(dwRule["QTYEND"].ToString());
            }
            if (QTYEND < 0)//如果用户没有设定
            {
                QTYEND = Convert.ToDouble(dwVKWDECK["WDBEDARF"].ToString()) / 100;
            }
            else
            {
                QTYEND = QTYEND;
            }
            QTYMAT = Math.Ceiling((QTYEND + F01) * (1 + R01));//F01浆损 R01浆伸
            if (DELDATE.Length < 8)
            {
                DELDATE = date;
            }
            #endregion

            //根据需求创建对应生产单信息，并且表达计划对应关系            
            dwMU = tbABAUFM.NewRow();
            dwMU.ItemArray = NewABA().ItemArray;

            dwMU["ABAID"] = ABAID;
            dwMU["ABAMANDANT"] = app.Mandant;
            dwMU["ABAVKAUFTYP"] = dwVKWDECK["WDSA"];
            dwMU["ABAVKAUFKRS"] = dwVKWDECK["WDAUFKRS"];
            dwMU["ABAVKAUFLFD"] = dwVKWDECK["WDAUFLFD"];
            dwMU["ABAVKAUFPOS"] = dwVKWDECK["WDAUFPOS"];
            dwMU["ABAVKUPOS"] = dwVKWDECK["WDUPOS"];
            if (dwMU["ABAVKUPOS"].ToString().Trim() == "00")
            {
                dwMU["ABAVKUPOS"] = "01";
            }
            dwMU["ABAVB"] = dwVKWDECK["VKPVB"];
            dwMU["ABAVG"] = dwVKWDECK["VKPVG"];
            dwMU["ABAVLVB"] = dwVKWDECK["VKPVB"];
            dwMU["ABAVLVG"] = dwVKWDECK["VKPVG"];
            dwMU["ABARVSACHB"] = dwVKWDECK["VKPSACHBE"];
            dwMU["ABAVLSACHB"] = dwVKWDECK["VKPSACHBE"];
            dwMU["ABARKUNDNR"] = dwVKWDECK["VKPKUNDNR"];
            dwMU["ABARKUNDNR"] = dwVKWDECK["VKPKUNDNR"];
            dwMU["ABAVLVB"] = dwVKWDECK["VKPVB"];
            dwMU["ABAVLVG"] = dwVKWDECK["VKPVG"];
            dwMU["REMARK"] = dwVKPOS["REMARK"];
            dwMU["F02DES"] = dwVKPOS["REPEATORDER"];
            dwMU["F02"] = dwVKPOS["PREALLOCATED"];
            dwMU["R01"] = dwRule["R01"];
            dwMU["R01DES"] = dwRule["R01DES"];
            dwMU["R02"] = dwRule["R02"];
            dwMU["JNUM"] = dwRule["JNUM"];
            dwMU["JWASTGE"] = dwRule["JWASTGE"];
            dwMU["UMS"] = dwRule["UMS"];
            dwMU["UNITNUM"] = dwRule["UNITNUM"];
            dwMU["TTENDS"] = dwRule["TTENDS"];
            dwMU["UENDS"] = dwRule["UENDS"];
            dwMU["DELDATE"] = dwRule["DELDATE"];
            dwMU["QTYEND"] = dwRule["QTYEND"];
            dwMU["ABARESMG"] = QTYMAT * 1000;
            dwMU["ABAAUFMSL"] = dwVKWDECK["VKPAUFMART"];
            dwMU["ABASSTKLG"] = dwVKWDECK["VKPLAEVON"];
            dwMU["EXART"] = krs + "-" + Helpers.Helper.Right(lfd, 4) + "-" + Helpers.Helper.Right(pos, 3);
            dwMU["ABASANZSTK"] = QTYMAT;
            dwMU["ABAWAKZ"] = dwVKWDECK["WDWAKZ"];
            dwMU["ABAART"] = dwVKWDECK["WDART"];
            dwMU["ABAVAR"] = dwVKWDECK["WDVAR"];
            dwMU["ABAFARBE"] = dwVKWDECK["WDFARBE"];
            dwMU["ABABESCHICH"] = dwVKWDECK["WDBESCHICH"];

            dwMU["ABAVKENDD"] = DELDATE;
            dwMU["ABAVKSTARTD"] = DELDATE;
            dwMU["ABAUSER"] = app.User;
            dwMU["ABADATE"] = date;
            tbABAUFM.Rows.Add(dwMU);
            ABAID++;
            dwRule = null;
            if (ABPLFD == "X")
                ABPLFD = INTEXBL.GetAUFLFD("JH", "ABKOPF");
            qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                     where row["ARTSTAGE"].ToString().Trim() == "F"
                     select row;

            foreach (DataRow row in qRules)
            {
                dwRule = row;
            }
            SResult = PlanAB05(SetEnv, db, dwMU, dwRule);
            SReturn.Merge(SResult);
            filter = "ABAAUFTYP='N' AND ABAWAKZ='R'";
            rows = tbABAUFM.Copy().Select(filter);
            qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                     where row["ARTSTAGE"].ToString().Trim() == "R"
                     select row;

            foreach (DataRow row in qRules)
            {
                dwRule = row;
            }
            foreach (DataRow dwAB55 in rows)
            {
                SResult = PlanAB55(SetEnv, db, dwAB55, dwRule);
                SReturn.Merge(SResult);
            }
            filter = "ABAAUFTYP='N' AND ABAWAKZ='W'";
            rows = tbABAUFM.Copy().Select(filter);
            qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                     where row["ARTSTAGE"].ToString().Trim() == "W"
                     select row;

            foreach (DataRow row in qRules)
            {
                dwRule = row;
            }
            foreach (DataRow dwAB45 in rows)
            {
                SResult = PlanAB45(SetEnv, db, dwAB45, dwRule);
                SReturn.Merge(SResult);
            }
            filter = "ABAAUFTYP='N' AND ABAWAKZ='G' AND ABPWAKZ<>'G'";
            string sort = "ABAVKAUFKRS ASC, ABAVKUPOS ASC";
            rows = tbABAUFM.Copy().Select(filter,sort);
            dwRule = null;
            qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                     where row["ARTSTAGE"].ToString().Trim() == "G"
                     select row;
            foreach (DataRow row in qRules)
            {
                dwRule = row;
            }
            foreach (DataRow dwAB50 in rows)
            {
                SResult = PlanAB50(SetEnv, db, dwAB50, dwRule);
                SReturn.Merge(SResult);
            }
            SReturn.Merge(tbABAUFM);
            return SReturn;
        }

        public DataSet Combine(DataSet SetEnv, DataSet SetData, double RQTY, string DD)
        {
            DataSet SReturn = new DataSet();
            DataRow dwABA = null;
            DataRow dwMU = null;
            foreach (DataRow row in SetData.Tables["ICC_ABAX"].Rows)
            {
                dwMU = row;
                //删除原来的生产单
                string client = dwMU["ABAMANDANT"].ToString();
                string krs = dwMU["ABAAUFKRS"].ToString();
                string lfd = dwMU["ABAAUFLFD"].ToString();
                string pos = dwMU["ABAAUFPOS"].ToString();
                INTEXBL.DELTEABPOS(client, krs, lfd, pos);
            }
            tbABAUFM = SetData.Tables["ICC_ABAX"].Clone();
            dwABA = tbABAUFM.Rows.Add();
            dwABA["ABAID"] = ABAID; ABAID++;
            dwABA["ABAMANDANT"] = dwMU["ABAMANDANT"];
            dwABA["ABAAUFTYP"] = "N";
            dwABA["ABAAUFKRS"] = dwMU["ABAAUFKRS"];
            dwABA["ABAAUFLFD"] = dwMU["ABAAUFLFD"];
            dwABA["ABAAUFPOS"] = dwMU["ABAAUFPOS"];
            dwABA["ABAVKAUFTYP"] = "V";
            dwABA["ABAVKAUFKRS"] = dwMU["ABAVKAUFKRS"];
            dwABA["ABAVKAUFLFD"] = dwMU["ABAVKAUFLFD"];
            dwABA["ABAVKAUFPOS"] = dwMU["ABAVKAUFPOS"];
            dwABA["ABAVKUPOS"] = "01";
            dwABA["EXART"] = dwMU["EXART"];
            dwABA["ABPWAKZ"] = dwMU["ABPWAKZ"];
            dwABA["ABPART"] = dwMU["ABPART"];
            dwABA["ABPVAR"] = dwMU["ABPVAR"];
            dwABA["ABPFARBE"] = dwMU["ABPFARBE"];
            dwABA["ABAWAKZ"] = dwMU["ABAWAKZ"];
            dwABA["ABAART"] = dwMU["ABAART"];
            dwABA["ABAVAR"] = dwMU["ABAVAR"];
            dwABA["ABAFARBE"] = dwMU["ABAFARBE"];
            dwABA["ABASSTKLG"] = 0;
            dwABA["ABARESMG"] = RQTY * 10;
            dwABA["ABASANZSTK"] = RQTY/100;

            dwABA["ABAVKENDD"] = dwMU["ABAVKENDD"];
            dwABA["ABAVKSTARTD"] = dwMU["ABAVKSTARTD"];
            dwABA["ABABASMG"] = 0;
            dwABA["ABADISPOKZ"] = "B";
            dwABA["ABALO"] = dwMU["ABALO"];
            dwABA["ABALB"] = dwMU["ABALB"];
            dwABA["ABALP"] = dwMU["ABALP"];
            dwABA["ABAUSER"] = app.User;
            dwABA["ABADATE"] = date;
            string filter = "ABAAUFTYP='N' AND ABAWAKZ='G'";
            //创建出新的单
            DataRow[] rows = tbABAUFM.Copy().Select(filter);
            DataRow dwRule = null;
            var qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                         where row["ARTSTAGE"].ToString().Trim() == "G"
                         select row;

            foreach (DataRow row in qRules)
            {
                row["DELDATE"] = "0";
                dwRule = row;
            }
            foreach (DataRow dwABAN in rows)
            {

                DataSet SResult = PlanAB50(SetEnv, SetData, dwABAN, dwRule);
                SReturn.Merge(SResult);
            }
            //创建新的需求关系
            DataRow dwABP_EXT = null;
            foreach (DataRow row in SReturn.Tables["ABPOS"].Rows)
            {
                dwABP_EXT = row;
            }
            DataTable tbMU = SetData.Tables["ICC_ABAX"].Copy();
            tbMU.TableName = "ICC_ABAX";
            int ABAAPOS = 2;
            foreach (DataRow row in tbMU.Rows)
            {
                row["ABAAUFKRS"] = dwABP_EXT["ABPAUFKRS"];
                row["ABAAUFLFD"] = dwABP_EXT["ABPAUFLFD"];
                row["ABAAUFPOS"] = dwABP_EXT["ABPAUFPOS"];
                row["ABAAPOS"] = ABAAPOS;
                ABAAPOS++;
            }
            foreach (DataRow row in tbABAUFM.Rows)
            {
                if (row["ABAAUFTYP"].ToString() == "N")
                {
                    dwMU = tbMU.Rows.Add();
                    dwMU.ItemArray = row.ItemArray;
                }
            }
            if (SReturn.Tables.Contains("ICC_ABAX"))
            {
                SReturn.Tables["ICC_ABAX"].Clear();
            }
            SReturn.Merge(tbMU);
            string path = @"C:\IntexReport\Data\JLTAV95";
            if (SReturn.Tables.Contains("ABPOS"))
            {
                SReturn.Tables["ABPOS"].TableName = "ABPOS_EXT";
                INTEXBL.Save2Xml(SReturn, path);
                INTEXBL.AutoConvert(path, "JLTAV95");
            }
            return SReturn;
        }
        public DataSet SplitAB(DataSet SetEnv, DataSet SetData, double RQTY, string DD)
        {
            DataSet SReturn = new DataSet();
            DataRow dwMU = null;
            DataRow dwABPN = null;
            double GAKG1 = 0;
            GAKG1 = RQTY * 1000;
            string ABPAUFPOS = "001";
            tbABAUFM = SetData.Tables["ICC_ABAX"].Clone();

            if (SetData.Tables["ICC_ABAX"].Rows.Count == 0)
                return SReturn;
            dwABPN = SetData.Tables["ICC_ABAX"].Rows[0];
            string client = dwABPN["ABAMANDANT"].ToString();
            string krs = dwABPN["ABAAUFKRS"].ToString();
            string lfd = dwABPN["ABAAUFLFD"].ToString();
            string pos = dwABPN["ABAAUFPOS"].ToString();
            DataRow dwData = INTEXDB.ICC_QUERYTB.NewRow();
            dwData["ABPMANDANT"] = dwABPN["ABAMANDANT"];
            dwData["ABPAUFKRS"] = dwABPN["ABAAUFKRS"];
            dwData["ABPAUFLFD"] = dwABPN["ABAAUFLFD"];
            DataSet SResult = INTEXDBC.QueryABPOS(dwData, 19);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                ABPAUFPOS = row["ABPAUFPOS"].ToString();
            }
            ABPAUFPOS = Helpers.Helper.Right("000" + (Convert.ToInt16(ABPAUFPOS) + 1), 3);
            #region======CurrentABP========

            dwMU = tbABAUFM.Rows.Add();
            dwMU.ItemArray = dwABPN.ItemArray;
            dwMU["ABAID"] = ABAID; ABAID++;
            double M5 = Convert.ToDouble(dwABPN["ABARESMG"].ToString());
            dwMU["ABAAUFTYP"] = "N";
            dwMU["ABARESMG"] = M5 - GAKG1;
            #endregion
            #region======NewABP========
            dwMU = tbABAUFM.Rows.Add();
            dwMU.ItemArray = dwABPN.ItemArray;
            dwMU["ABAID"] = ABAID; ABAID++;
            dwMU["ABAAUFTYP"] = "N";
            dwMU["ABAAUFPOS"] = ABPAUFPOS;
            dwMU["ABARESMG"] = GAKG1;
            dwMU["ABAUSER"] = app.User;
            dwMU["ABADATE"] = date;
            #endregion
            string filter = "ABAAUFTYP='N' AND ABAWAKZ='G'";
            DataRow[] rows = tbABAUFM.Copy().Select(filter);
            DataRow dwRule = null;
            var qRules = from DataRow row in SetEnv.Tables["PRULES"].AsEnumerable()
                         where row["ARTSTAGE"].ToString().Trim() == "G"
                         select row;

            foreach (DataRow row in qRules)
            {
                row["DELDATE"] = "0";
                dwRule = row;
            }
            foreach (DataRow dwABA in rows)
            {
                SResult = PlanAB50(SetEnv, SetData, dwABA, dwRule);
                SReturn.Merge(SResult);
            }
            SReturn.Merge(tbABAUFM);
            string path = @"C:\IntexReport\Data\JLTAV95";
            if (SReturn.Tables.Contains("ABPOS"))
            {
                SReturn.Tables["ABPOS"].TableName = "ABPOS_EXT";
                INTEXBL.Save2Xml(SReturn, path);
                INTEXBL.AutoConvert(path, "JLTAV95");
            }
            INTEXBL.DELTEABPOS(client, krs, lfd, pos);
            return SReturn;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="db"></param>
        /// 遍历ZUTEI，相同箱子合并，更改生产单号码，
        /// 创建新的生产单
        /// 修复新生产单和JH单之间的关系
        /// 建立ZUTEI与计划单之间的关系
        /// <returns></returns>
        public DataSet LSYarn(DataSet db)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataRow dwABAN;
            string _KRS, _LFD, _POS;
            DataTable tbMU = db.Tables["ICC_ABAX"];
            DataTable tbZUITX = db.Tables["ZUTEI"].Clone();
            tbZUITX.TableName = "ZUTEI_ITX";
            if (db.Tables.Contains("ZUTEI_ITX"))
            {
                db.Tables["ZUTEI_ITX"].Clear();
            }
            db.Merge(tbZUITX);
            tbZUITX = db.Tables["ZUTEI_ITX"];
            Dictionary<string, DataRow> ZUTEI = new Dictionary<string, DataRow>();
            try
            {
                ABPLFD = INTEXBL.GetAUFLFD("FP", "ABKOPF");
                string filter = "";
                DataRow[] rows;
                DataRow dwZUTEI;
                string YART = "", YVAR = "", YCOLOR = "";
                string PROCESSED = "";
                ABPOS = 1;
                foreach (DataRow dwZU in db.Tables["ZUTEI"].Rows)
                {
                    #region====合并留位的箱子=====
                    _KRS = dwZU["ZUAUFKRS"].ToString();
                    _LFD = dwZU["ZUAUFLFD"].ToString();
                    _POS = dwZU["ZUAUFPOS"].ToString();
                    YART = dwZU["ZUART"].ToString();
                    YVAR = dwZU["ZUVAR"].ToString();
                    YCOLOR = dwZU["ZUFARBE"].ToString();
                    string ZULBEHI = dwZU["ZULBEHI"].ToString();
                    double ZUEINTKG = Convert.ToDouble(dwZU["ZUEINTKG"].ToString());
                    double ZUENTNKG = Convert.ToDouble(dwZU["ZUENTNKG"].ToString());
                    if (ZUENTNKG > 0) continue;
                    if (ZUTEI.ContainsKey(ZULBEHI))//箱子是否存在
                    {
                        //若存在，则增加数量
                        dwZUTEI = ZUTEI[ZULBEHI];
                        dwZUTEI["ZUEINTKG"] = ZUEINTKG + Convert.ToDouble(dwZUTEI["ZUEINTKG"].ToString());
                        dwZUTEI["ZUENTNKG"] = ZUENTNKG + Convert.ToDouble(dwZUTEI["ZUENTNKG"].ToString());
                    }
                    else
                    {
                        dwZUTEI = dwZU;
                        //若不存在，坚持相同纱支是否存在
                        if (!PROCESSED.Contains(YART + YVAR + YCOLOR + ";"))
                        {
                            //若不存在，则建立新的生产单行 
                            dwZUTEI["ZUAUFKRS"] = "FP";
                            dwZUTEI["ZUAUFLFD"] = ABPLFD;
                            dwZUTEI["ZUAUFPOS"] =Helpers.Helper.Right("000"+ABPOS.ToString(),3);
                            ABPOS++;
                            PROCESSED += YART + YVAR + YCOLOR + ";";
                        }
                        else
                        {
                            //若存在，则取出生产单号码
                            foreach (KeyValuePair<string, DataRow> kp in ZUTEI)
                            {
                                if (kp.Value["ZUART"].ToString() == YART && kp.Value["ZUVAR"].ToString() == YVAR && kp.Value["ZUFARBE"].ToString() == YCOLOR)
                                {
                                    dwZUTEI["ZUAUFKRS"] = kp.Value["ZUAUFKRS"];
                                    dwZUTEI["ZUAUFLFD"] = kp.Value["ZUAUFLFD"];
                                    dwZUTEI["ZUAUFPOS"] = kp.Value["ZUAUFPOS"];
                                }
                            }
                        }
                        ZUTEI.Add(ZULBEHI, dwZUTEI);
                    }
                    #endregion
                    #region===处理计划单的ICC_ABAX==========
                    filter = "ABAVKAUFKRS='" + _KRS + "' AND ABAVKAUFLFD='" + _LFD + "' AND ABAVKAUFPOS='" + _POS + "'";
                    rows = tbMU.Select(filter);
                    foreach (DataRow MU in rows)
                    {
                        MU["ABAAUFTYP"] = "V";
                        MU["ABAAUFKRS"] = dwZUTEI["ZUAUFKRS"];
                        MU["ABAAUFLFD"] = dwZUTEI["ZUAUFLFD"];
                        MU["ABAAUFPOS"] = dwZUTEI["ZUAUFPOS"];
                    }
                    #endregion
                }

                #region===创建独立的ABPOS==========

                foreach (KeyValuePair<string, DataRow> kp in ZUTEI)
                {
                    dwZUTEI = tbZUITX.Rows.Add();
                    dwZUTEI.ItemArray = kp.Value.ItemArray;

                    _KRS = dwZUTEI["ZUAUFKRS"].ToString();
                    _LFD = dwZUTEI["ZUAUFLFD"].ToString();
                    _POS = dwZUTEI["ZUAUFPOS"].ToString();

                    dwABAN = tbMU.Rows.Add();
                    dwABAN["ABAID"] = ABAID++;
                    dwABAN["ABAMANDANT"] = dwZUTEI["ZUMANDANT"];
                    dwABAN["ABAAUFTYP"] = "N";
                    dwABAN["ABAAUFKRS"] = "00";
                    dwABAN["ABAAUFLFD"] = "000000";
                    dwABAN["ABAAUFPOS"] = "000";
                    dwABAN["ABAVKAUFTYP"] = "V";
                    dwABAN["ABAVKAUFKRS"] = _KRS;
                    dwABAN["ABAVKAUFLFD"] = _LFD;
                    dwABAN["ABAVKAUFPOS"] = _POS;
                    
                    dwABAN["ABAVKUPOS"] = dwZUTEI["ZUPOS"].ToString();
                    if (dwABAN["ABAVKUPOS"].ToString().Trim() == "00")
                    {
                        dwABAN["ABAVKUPOS"] = "01";
                    }
                    dwABAN["ABAAUFMSL"] = "01"; //GAAUFMSL
                    dwABAN["ABAPACKTYP"] = "BZD ";
                    dwABAN["ABAHUELSE"] = "      ";

                    dwABAN["ABARESMG"] = dwZUTEI["ZUEINTKG"];
                    dwABAN["ABAQTYRECEIVED"] = dwZUTEI["ZUENTNKG"];
                    dwABAN["ABASANZSTK"] = Convert.ToDouble(dwZUTEI["ZUEINTKG"].ToString()) / 1000;
                    dwABAN["ABASSTKGEW"] = "1000";
                    filter = "ABAAUFKRS='" + _KRS + "' AND ABAAUFLFD='" + _LFD + "' AND ABAAUFPOS='" + _POS + "'";
                    rows = tbMU.Select(filter);
                    DataRow dwAUF = null;

                    if (rows.Length > 0)
                    {
                        dwAUF = rows[0];
                        dwABAN["ABALO"] = "01";// dwAUF["ABALO"];
                        dwABAN["ABALB"] = "SC"; //dwAUF["ABALB"];
                        dwABAN["ABALP"] = "01      ";// dwAUF["ABALP"];
                        dwABAN["EXART"] =Helpers.Helper.Left( dwAUF["EXART"].ToString().PadRight(7),7);
                    }
                    dwABAN["ABAWAKZ"] = "G";
                    dwABAN["ABAART"] = dwZUTEI["ZUART"];
                    dwABAN["ABAVAR"] = dwZUTEI["ZUVAR"];
                    dwABAN["ABAFARBE"] = dwZUTEI["ZUFARBE"];
                    dwABAN["ABAVKSTARTD"] = date;

                    dwABAN["ABAUSER"] = app.User;
                    dwABAN["ABADATE"] = date;
                }

                filter = "ABAAUFTYP='N' AND ABAWAKZ='G'";
                rows = tbMU.Copy().Select(filter);
                if (rows.Length > 0)
                {
                    foreach (DataRow dwABA in rows)
                    {
                        SResult = INTEXBL.PlanAB50MU(db, dwABA, ref ABAID);
                        SReturn.Merge(SResult);
                    }
                }
                filter = "ABAAUFTYP='V'";
                rows = tbMU.Copy().Select(filter);
                if (rows.Length > 0)
                {
                    DataTable tb = rows.CopyToDataTable();
                    tb.TableName = "ICC_ABAX";
                    int ABAAPOS = 2;
                    foreach (DataRow row in tb.Rows)
                    {
                        row["ABAAPOS"] = Helpers.Helper.Right("0000" + ABAAPOS, 4);
                        ABAAPOS++;
                    }
                    SReturn.Merge(tb);
                }
                #endregion
                #region===处理计划单的GAAUF==========
                #endregion
                #region===处理计划单的ABPOS==========
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
    }
}
